******
* author: Anthony Harding (tony.harding@gatech.edu)
* Georgia Institute of Technology, School of Public Policy


* This script generates regression coefficients and tables
* Requires estout, estwide, ftools, ivreg2, reghdfe, and ivreghdfe packages

* 1st Imports Data
* 2nd Runs baseline regressions
* Outputs data for Table 1
******


clear all
set mem 1G
set matsize 10000
set maxvar 10000

* Set working directory
cd "\\Client\C$\Users\aharding6\GaTech Dropbox\Anthony Harding\Tobetransferred\ConsistentClimateConvergence - Copy\Replication"
*cd "\\Client\C$\Users\tonyh\GaTech Dropbox\Anthony Harding\Tobetransferred\ConsistentClimateConvergence - Copy\Replication"



*-------------------------------------------------------------------------------
*-------------------------------------------------------------------------------
* 1. Import and prep data
*-------------------------------------------------------------------------------
*-------------------------------------------------------------------------------

*-------------------------------------------------------------------------------
* Step 1 - Import main datasets and merge
*-------------------------------------------------------------------------------
*******
* Temperature from MWv5
*******

import delimited data/output/MWv5/CountryTemp.csv, varnames(1) clear
drop if gmi_cntry == "REU" | gmi_cntry == "UMI"
drop if gmi_cntry == "ISR" & cntry_name != "Israel"
drop if gmi_cntry == "NOR" & cntry_name != "Norway"
reshape long temp, i(gmi_cntry) j(year)
replace temp = "." if temp == "NA"
destring temp, replace
rename temp tempMWv5
rename gmi_cntry iso
tempfile tempMWv5
save `tempMWv5'


*******
* Precipitation from MWv5
*******

import delimited data/output/MWv5/CountryPrecip.csv, varnames(1) clear
drop if gmi_cntry == "REU" | gmi_cntry == "UMI"
drop if gmi_cntry == "ISR" & cntry_name != "Israel"
drop if gmi_cntry == "NOR" & cntry_name != "Norway"
reshape long precip, i(gmi_cntry) j(year)
replace precip = "." if precip == "NA"
destring precip, replace
rename precip precMWv5
rename gmi_cntry iso
tempfile precipMWv5
save `precipMWv5'


*******
* Temperature innovation from MWv5
*******

import delimited data/output/MWv5TemperatureInnovation.csv, varnames(1) clear
keep year mwv5innovation
replace mwv5innovation = "." if mwv5innovation == "NA"
destring mwv5innovation, replace
tempfile mwv5innovation
save `mwv5innovation'


*******
* Temperature innovation from NOAA
*******

import delimited data/output/NOAATemperatureInnovation.csv, varnames(1) clear
keep year noaainnovation anomaly
replace noaainnovation = "." if noaainnovation == "NA"
destring noaainnovation, replace
tempfile noaainnovation
save `noaainnovation'

*******
* Global GDP from WDI
*******
import delimited Data/Input/GDP_Constant_wdi.csv, varnames(1) clear
local i=1960
foreach var of varlist v5-v68{
	rename `var' GDP_WLD`i'
	local i = `i'+1
}
drop indicatorname indicatorcode
reshape long GDP_WLD, i(countrycode) j(year)
keep if countrycode=="WLD"
keep year GDP_WLD
duplicates drop
tempfile GDP_WLD
save `GDP_WLD'

*******
* TotGDP from WDI
*******

import delimited Data/Input/GDP_Constant_wdi.csv, varnames(1) clear
local i=1960
foreach var of varlist v5-v68{
	rename `var' TotGDP`i'
	local i = `i'+1
}
drop indicatorname indicatorcode
reshape long TotGDP, i(countrycode) j(year)
rename countrycode iso
replace iso = "ROM" if iso == "ROU"
replace iso = "ZAR" if iso == "COD"
tempfile TotGDP
save `TotGDP'

*******
* Global GDP/capita from WDI
*******

import delimited data/input/GDPcap_wdi.csv, varnames(1) clear
local i=1960
foreach var of varlist v5-v68{
	rename `var' gdpCAP_WLD`i'
	local i = `i'+1
}
drop indicatorname indicatorcode
reshape long gdpCAP_WLD, i(countrycode) j(year)
keep if countrycode == "WLD"
duplicates drop
keep year gdpCAP_WLD
tempfile gdpCAP_WLD
save `gdpCAP_WLD'


*******
* GDP/capita from WDI
*******

import delimited data/input/GDPcap_wdi.csv, varnames(1) clear
local i=1960
foreach var of varlist v5-v68{
	rename `var' gdpCAP`i'
	local i = `i'+1
}
drop indicatorname indicatorcode
reshape long gdpCAP, i(countrycode) j(year)
rename countrycode iso
replace iso = "ROM" if iso == "ROU"
replace iso = "ZAR" if iso == "COD"
encode iso, g(iso_id)
xtset iso_id year
gen growthgdpCAP = log(gdpCAP) - log(l.gdpCAP)
drop iso_id



*******
* Merge data
*******
merge 1:1 iso year using `TotGDP'
drop _merge

merge 1:1 iso year using `tempMWv5'
drop _merge

merge 1:1 iso year using `precipMWv5'
drop _merge

merge m:1 year using `mwv5innovation'
drop _merge

merge m:1 year using `noaainnovation'
drop _merge

merge m:1 year using `GDP_WLD'
drop _merge

merge m:1 year using `gdpCAP_WLD'
drop _merge


*-------------------------------------------------------------------------------
* End Step 1 - End Step 1 - End Step 1 - End Step 1 - End Step 1 - End Step 1 
*-------------------------------------------------------------------------------


*-------------------------------------------------------------------------------
* Step 2 - Generate new useful variables
*-------------------------------------------------------------------------------
* Generate country indicator and xtset data
encode iso, g(iso_id)
xtset iso_id year


* New economic variables
gen log_gdpCAP_wdi = log(gdpCAP)
gen loggdpCAP_wdi_l = l.log_gdpCAP
rename gdpCAP gdpCAP_wdi
rename growthgdpCAP growthWDI

* Create Rich/poor dummy
preserve
drop if missing(gdpCAP_wdi)
bys iso_id: keep if _N > 19 
bys iso_id: keep if _n == 1 
xtile gdpCAP_tile = gdpCAP_wdi , nq(2)
keep iso_id gdpCAP_tile
tempfile tempxtile
save `tempxtile',replace
restore
mmerge iso_id using `tempxtile', type(n:1)
gen poor = 0
replace poor = 1 if gdpCAP_tile==1
drop _merge


* New climate variables
rename tempMWv5 temp
rename precMWv5 prec
replace prec = prec/1000 
gen temp2 = temp^2
gen prec2 = prec^2

sort iso_id year
gen dtemp = temp - l.temp
gen dprec = prec - l.prec
gen dtemp2 = temp2 - l.temp2
gen dprec2 = prec2 - l.prec2

gen poor_temp = poor*temp
gen poor_prec = poor*prec
gen poor_temp2 = poor*temp2
gen poor_prec2 = poor*prec2
gen poor_dtemp = poor*dtemp
gen poor_dprec = poor*dprec
gen poor_dtemp2 = poor*dtemp2
gen poor_dprec2 = poor*dprec2


* Generate new year variables
gen time = year-1960
gen timegr = time/(1+time)
gen timegr2 = 1/2*time^2/(1+time)
gen time2 = time^2
gen year2 = year^2
gen yeargr = year/(1+year)

*-------------------------------------------------------------------------------
* End Step 2 - End Step 2 - End Step 2 - End Step 2 - End Step 2 - End Step 2 
*-------------------------------------------------------------------------------

*-------------------------------------------------------------------------------
* Step 3 - Rename and relabel key variables
*-------------------------------------------------------------------------------

* Relabel variables
label variable temp "Temp"
label variable temp2 "Temp Sq."
label variable prec "Precip"
label variable prec2 "Precip Sq."
label variable dtemp "$\Delta$Temp"
label variable dtemp2 "$\Delta$Temp Sq."
label variable dprec "$\Delta$Precip"
label variable dprec2 "$\Delta$Precip Sq."
label variable poor "poor"
label variable poor_temp "Temp*Poor"
label variable poor_prec "Precip*Poor"
label variable poor_temp2 "Temp Sq.*Poor"
label variable poor_prec2 "Precip Sq.*Poor"
*-------------------------------------------------------------------------------
* End Step 3 - End Step 3 - End Step 3 - End Step 3 - End Step 3 - End Step 3 
*-------------------------------------------------------------------------------

*-------------------------------------------------------------------------------
* Step 4 - Export prepped dataset for projections
*-------------------------------------------------------------------------------
* Save as .dta file

save "data/output/PreppedDataset_UpdatedData", replace

preserve
keep year iso iso_id temp prec growthWDI gdpCAP_wdi loggdpCAP_wdi_l TotGDP
export delimited using "data/output/mainDataset_UpdatedData.csv", replace
restore

*-------------------------------------------------------------------------------
* End Step 4 - End Step 4 - End Step 4 - End Step 4 - End Step 4 - End Step 4 
*-------------------------------------------------------------------------------



*-------------------------------------------------------------------------------
*-------------------------------------------------------------------------------
* 2. Run and Store Regressions
*-------------------------------------------------------------------------------
*-------------------------------------------------------------------------------


*-------------------------------------------------------------------------------
* Step 1 - Pooled SR, no country time trend, no GDP lag
*-------------------------------------------------------------------------------
*** Column 1 ***
eststo: reghdfe growthWDI temp temp2 prec prec2, absorb(i.year  i.iso_id) vce(cluster iso_id)
estadd local TimeTrend "None", replace
estadd local RichPoor "Pooled", replace
estadd local MaxTemp=round(_b[temp]/(-2*_b[temp2]),0.1), replace

*-------------------------------------------------------------------------------
* End Step 1 - End Step 1 - End Step 1 - End Step 1 - End Step 1 - End Step 1 
*-------------------------------------------------------------------------------

*-------------------------------------------------------------------------------
* Step 2 - Pooled SR, linear country time trend, no GDP lag
*-------------------------------------------------------------------------------
*** Column 2 ***
eststo: reghdfe growthWDI temp temp2 prec prec2, absorb(i.year  i.iso_id i.iso_id#c.time) vce(cluster iso_id)
estadd local TimeTrend "Linear", replace
estadd local RichPoor "Pooled", replace
estadd local MaxTemp=round(_b[temp]/(-2*_b[temp2]),0.1), replace

*-------------------------------------------------------------------------------
* End Step 2 - End Step 2 - End Step 2 - End Step 2 - End Step 2 - End Step 2 
*-------------------------------------------------------------------------------


*-------------------------------------------------------------------------------
* Step 3 - Pooled SR, quadratic country time trend, no GDP lag
*-------------------------------------------------------------------------------
*** Column 3 ***
eststo: reghdfe growthWDI temp temp2 prec prec2, absorb(i.year  i.iso_id i.iso_id#c.time i.iso_id#c.time2) vce(cluster iso_id)
estadd local TimeTrend "Quadratic", replace
estadd local RichPoor "Pooled", replace
estadd local MaxTemp=round(_b[temp]/(-2*_b[temp2]),0.1), replace

*-------------------------------------------------------------------------------
* End Step 3 - End Step 3 - End Step 3 - End Step 3 - End Step 3 - End Step 3 
*-------------------------------------------------------------------------------




*-------------------------------------------------------------------------------
* Step 4 - Pooled SR, linear country time trend, no GDP lag, global temperature innovation
*-------------------------------------------------------------------------------
*** Column 4 ***
eststo: reghdfe growthWDI temp temp2 prec prec2 l(0/5).noaainnovation, absorb(i.iso_id i.iso_id#c.year) cluster(iso_id)
estadd local TimeTrend "Linear", replace
estadd local RichPoor "Pooled", replace
estadd local MaxTemp=round(_b[temp]/(-2*_b[temp2]),0.1), replace

*-------------------------------------------------------------------------------
* End Step 4 - End Step 4 - End Step 4 - End Step 4 - End Step 4 - End Step 4 
*-------------------------------------------------------------------------------


*-------------------------------------------------------------------------------
* Step 5 - Pooled SR, no country time trend, 1 GDP lag
*-------------------------------------------------------------------------------
*** Column 5 ***
eststo: ivreghdfe growthWDI temp temp2 prec prec2 (l.log_gdpCAP_wdi = l3.log_gdpCAP_wdi), absorb(i.year  i.iso_id)  cluster(iso_id)
estadd local TimeTrend "None", replace
estadd local RichPoor "Pooled", replace
estadd local MaxTemp=round(_b[temp]/(-2*_b[temp2]),0.1), replace

*-------------------------------------------------------------------------------
* End Step 5 - End Step 5 - End Step 5 - End Step 5 - End Step 5 - End Step 5
*-------------------------------------------------------------------------------


*-------------------------------------------------------------------------------
* Step 6 - Pooled SR, linear country time trend, 1 GDP lag
*-------------------------------------------------------------------------------
*** Column 6 ***
eststo: ivreghdfe growthWDI temp temp2 prec prec2 (l.log_gdpCAP_wdi = l3.log_gdpCAP_wdi), absorb(i.year  i.iso_id i.iso_id#c.time) cluster(iso_id)
estadd local TimeTrend "Linear", replace
estadd local RichPoor "Pooled", replace
estadd local MaxTemp=round(_b[temp]/(-2*_b[temp2]),0.1), replace

*-------------------------------------------------------------------------------
* End Step 6 - End Step 6 - End Step 6 - End Step 6 - End Step 6 - End Step 6
*-------------------------------------------------------------------------------


*-------------------------------------------------------------------------------
* Step 7 - Pooled SR, linear country time trend, 1 GDP lag, global temperature innovation
*-------------------------------------------------------------------------------
*** Column 7 ***
eststo: ivreghdfe growthWDI temp temp2 prec prec2 l(0/5).noaainnovation (l.log_gdpCAP_wdi = l6.log_gdpCAP_wdi l7.log_gdpCAP_wdi), absorb(i.iso_id i.iso_id#c.year) cluster(iso_id)
estadd local TimeTrend "Linear", replace
estadd local RichPoor "Pooled", replace
estadd local MaxTemp=round(_b[temp]/(-2*_b[temp2]),0.1), replace

*-------------------------------------------------------------------------------
* End Step 7 - End Step 7 - End Step 7 - End Step 7 - End Step 7 - End Step 7
*-------------------------------------------------------------------------------

*-------------------------------------------------------------------------------
* Step 8 - Pooled LR, convergent country time trend, 1 GDP lag
*-------------------------------------------------------------------------------
*** Column 8 ***
eststo: ivreghdfe growthWDI dtemp dtemp2 dprec dprec2 temp temp2 prec prec2 (l.growthWDI = l3.growthWDI), absorb(i.year  i.iso_id) cluster(iso_id)
estadd local TimeTrend "None", replace
estadd local RichPoor "Pooled", replace
estadd local MaxTemp=round(_b[dtemp]/(-2*_b[dtemp2]),0.1), replace

*-------------------------------------------------------------------------------
* End Step 8 - End Step 8 - End Step 8 - End Step 8 - End Step 8 - End Step 8
*-------------------------------------------------------------------------------


*-------------------------------------------------------------------------------
* Step 9 - Rich/poor LR, no country time trend, 1 GDP lag
*-------------------------------------------------------------------------------
*** Column 9 ***
eststo: ivreghdfe growthWDI i.poor#c.(dtemp dtemp2 dprec dprec2 temp temp2 prec prec2) (i.poor#c.l.growthWDI = i.poor#c.l3.growthWDI), absorb(i.year  i.iso_id) cluster(iso_id)
estadd local TimeTrend "None", replace
estadd local RichPoor "Rich/Poor", replace
estadd local MaxTemp=round(_b[1.poor#c.dtemp]/(-2*_b[1.poor#c.dtemp2]),0.1), replace

*-------------------------------------------------------------------------------
* End Step 9 - End Step 9 - End Step 9 - End Step 9 - End Step 9 - End Step 9
*-------------------------------------------------------------------------------


*-------------------------------------------------------------------------------
* Step 10 - Export Regression Coefficients
*-------------------------------------------------------------------------------
* Export coefficients for tables and figures
esttab using "data/output/RegressionCoeff_Text_UpdatedData.csv", drop(_cons) not noobs plain replace

* Export regression results to LaTex Table
estwide using "tablesandfigures/RegressionCoeff_Text_UpdatedData.tex", se ar2 drop(_cons) s(MaxTemp TimeTrend N r2 r2_a, label("Max GDP Growth Temp" "Country-Specific Time Trend" "Obs."  "R sq." "Adj. R sq.")) label addnote("All models include country and year fixed effects. Standard Errors are clustered at the country level. Temperature is measured in C. Precipitation is measured in $\mu$m/year") replace

* Clear results
eststo clear
*-------------------------------------------------------------------------------
* End Step 10 - End Step 10 - End Step 10 - End Step 10 - End Step 10 - End Step 10
*-------------------------------------------------------------------------------



